package edu.sdcet.demo1013.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.sdcet.demo1013.entity.Student;
import edu.sdcet.demo1013.mapper.StudentMapper;
import edu.sdcet.demo1013.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);

    //注入StudentMapper
    @Autowired
    private StudentMapper studentMapper;

    public Student getStudentById(String StudentId) {
        logger.info("查询学生信息：studentId={}", StudentId);
        // 使用MyBatis-Plus的selectById方法
        Student student = studentMapper.selectById(StudentId);
        logger.info("查询结果：student={}", student);
        return student;
    }

    //查询所有学生信息
    public List<Student> getAllStudents() {
        List<Student> students = studentMapper.selectList(null);
        logger.info("查询所有学生信息：学生数量={}", students.size());
        return students;
    }

    /**
     * 带参数的学生信息查询
     * @param status 状态（1：正常，0：已删除）
     * @return 符合条件的学生列表
     */
    public List<Student> getStudentsByCondition(Integer status) {
        // 创建QueryWrapper对象用于构建查询条件
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        // 添加状态查询条件
        queryWrapper.eq("status", status); // 等价于 SQL 中的 WHERE status = ?
        // 执行查询
        List<Student> students = studentMapper.selectList(queryWrapper);
        logger.info("查询结果：students={}", students);
        return students;
    }

    /**
     * 更新学生信息
     * @param student
     * @return void
     */
    public void updateStudent(Student student) {
        logger.info("更新学生信息：student={}", student);
        String studentId = student.getStudentId();
        // 使用MyBatis-Plus的updateById方法
        studentMapper.updateById(student);
    }

    /**
     * 新增学生信息
     */
    public void addStudent(Student student) {
        logger.info("新增学生信息：student={}", student);
        // 使用MyBatis-Plus的insert方法
        studentMapper.insert(student);
    }
}
